package com.zjw.touchscreen.bluetooth;

import android.annotation.SuppressLint;
import android.bluetooth.*;
import android.content.Context;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;
import com.zjw.touchscreen.R;
import com.zjw.touchscreen.adapter.BluetoothSpinnerAdapter;

import java.util.Set;

@SuppressLint("MissingPermission")
@RequiresApi(api = Build.VERSION_CODES.M)
public class BluetoothUtils {

    public static BluetoothAdapter bluetoothAdapter;

    public static BluetoothDevice bluetoothDevice;

    public static BluetoothHidDevice bluetoothHidDevice;

    public static BluetoothHeadset bluetoothHeadset;

    public static boolean isHidDeviceConnected = false;

    public static BluetoothAdapter getBluetoothAdapter() {
        return bluetoothAdapter;
    }

    public static void setBluetoothDevice(BluetoothDevice bluetoothDevice) {
        BluetoothUtils.bluetoothDevice = bluetoothDevice;
    }

    public static BluetoothDevice getBluetoothDevice() {
        return bluetoothDevice;
    }

    public static BluetoothHidDevice getBluetoothHidDevice() {
        return bluetoothHidDevice;
    }

    public static void setBluetoothHidDevice(BluetoothHidDevice bluetoothHidDevice) {
        BluetoothUtils.bluetoothHidDevice = bluetoothHidDevice;
    }

    public static void setBluetoothHeadset(BluetoothHeadset bluetoothHeadset) {
        BluetoothUtils.bluetoothHeadset = bluetoothHeadset;
    }

    public static BluetoothHeadset getBluetoothHeadset() {
        return bluetoothHeadset;
    }

    public static void init(Context context, BluetoothSpinnerAdapter bluetoothSpinnerAdapter){
        BluetoothManager bluetoothManager = context.getSystemService(BluetoothManager.class);
        bluetoothAdapter = bluetoothManager.getAdapter();
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        filter.addAction("android.bluetooth.input.profile.action.CONNECTION_STATE_CHANGED");
        BluetoothBroadcastReceiver bluetoothBroadcastReceiver = new BluetoothBroadcastReceiver(bluetoothSpinnerAdapter);
        context.registerReceiver(bluetoothBroadcastReceiver, filter);
        Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
        for (BluetoothDevice bondedDevice : bondedDevices) {
            BluetoothListItem bluetoothListItem = new BluetoothListItem();
            switch (bondedDevice.getBondState()) {
                case BluetoothDevice.BOND_BONDING:
                    bluetoothListItem.setStatus("正在配对");
                    break;
                case BluetoothDevice.BOND_BONDED:
                    bluetoothListItem.setStatus("配对成功");
                    break;
                case BluetoothDevice.BOND_NONE:
                    bluetoothListItem.setStatus("未配对");
                    break;
                default:
                    bluetoothListItem.setStatus("未知状态");
                    break;
            }
            Drawable icon=null;
            bluetoothListItem.setName(bondedDevice.getName());
            BluetoothClass bluetoothClass = bondedDevice.getBluetoothClass();
            int majorDeviceClass = bluetoothClass.getMajorDeviceClass();
            switch (majorDeviceClass) {
                //电脑设备
                case 0x0100:
                    icon = ContextCompat.getDrawable(context, R.drawable.computer);
                    break;
                //手机设备
                case 0x0200:
                    icon = ContextCompat.getDrawable(context, R.drawable.phone);
                    break;
                //音频视频设备，蓝牙耳机音响
                case 0x0400:
                    icon = ContextCompat.getDrawable(context, R.drawable.headset);
                    break;
                //外部设备，比如蓝牙鼠标键盘，蓝牙遥控器
                case 0x0500:
                    icon = ContextCompat.getDrawable(context, R.drawable.keyboard);
                    break;
                //可穿戴手表
                case 0x0700:
                    icon = ContextCompat.getDrawable(context, R.drawable.watch);
                default:
                    icon = ContextCompat.getDrawable(context, R.drawable.bluetooth);
            }
            bluetoothListItem.setAddr(bondedDevice.getAddress());
            bluetoothListItem.setIcon(icon);
            bluetoothListItem.setBluetoothDevice(bondedDevice);
            bluetoothSpinnerAdapter.refresh(bluetoothListItem);
        }

        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                bluetoothAdapter.startDiscovery();
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

    }
}
